//
//  AppDelegate.swift
//  FirstTV
//
//  Created by unisedu on 15/10/23.
//  Copyright © 2015年 CaoFei. All rights reserved.
//

import UIKit
import TVMLKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,TVApplicationControllerDelegate {

    var window: UIWindow?
    var appController: TVApplicationController?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        let appControllerContext = TVApplicationControllerContext()
        
        let javascriptURL = NSURL(string: "Enter path to your JavaScript file here")
        
        appControllerContext.javaScriptApplicationURL = javascriptURL!
        if let options = launchOptions {
            for (kind, value) in options {
                if let kindStr = kind as? String {
                    appControllerContext.launchOptions[kindStr] = value
                }
            }
        }
        
        self.appController = TVApplicationController(context: appControllerContext, window: self.window, delegate: self)
        
        
        
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
//    The code in the above example loads a JavaScript file that then loads a TVML page and displays it in the simulator or on a television screen if a new Apple TV is connected to your computer. For more information about JavaScript classes, see TVJS Framework Reference.https://developer.apple.com/library/prerelease/tvos/documentation/TVMLJS/Reference/TVJSFrameworkReference/index.html#//apple_ref/doc/uid/TP40016076
//    function getDocument(url) {
//    var templateXHR = new XMLHttpRequest();
//    templateXHR.responseType = "document";
//    templateXHR.addEventListener("load", function() {pushDoc(templateXHR.responseXML);}, false);
//    templateXHR.open("GET", url, true);
//    templateXHR.send();
//    return templateXHR;
//    }
//    
//    function pushDoc(document) {
//    navigationDocument.pushDocument(document);
//    }
//    
//    App.onLaunch = function(options) {
//    var templateURL = 'Enter path to your server here/alertTemplate.tvml';
//    getDocument(templateURL);
//    }
//    
//    App.onExit = function() {
//    console.log('App finished');
//    }
//    The above JavaScript loads a TVML page that displays an alert asking if the user wants to upgrade to the premium version of your app. The example below shows the TVML code to create the alert template page. The resulting TVML file is loaded by the JavaScript file and displayed on the TV screen. For more information on available TVML templates and elements, see Apple TV Markup Language Reference.https://developer.apple.com/library/prerelease/tvos/documentation/LanguagesUtilities/Conceptual/ATV_Template_Guide/index.html#//apple_ref/doc/uid/TP40015064
//    
//    <document>
//    <alertTemplate>
//    <title>Update to premium</title>
//    <description>Go ad free by updating to the premium version</description>
//    <button>
//    <text>Update Now</text>
//    </button>
//    <button>
//    <text>Cancel</text>
//    </button>
//    </alertTemplate>
//    </document>
}

